from app.models import Book
from app.serializers import BookSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import generics

# 方法一
class Booklist(generics.ListCreateAPIView):
    '''
    基于类的写法
    '''
    queryset = Book.objects.all()
    serializer_class = BookSerializer

# 方法二
# class Booklist(APIView):
#     def get(self, request, format=None):
#         books = Book.objects.all()
#         ser = BookSerializer(books, many=True)
#         return Response(ser.data)
#
#     def post(self, request, format=None):
#         ser = BookSerializer(request.DATA)
#         if ser.is_valid():
#             ser.save()
#             return Response(ser.data)
#         return Response(ser.errors)

class BookDetail(APIView):
    def get(self, request, num, format=None):
        b = Book.objects.get(id=num)
        ser = BookSerializer(b)
        return Response(ser.data)